:: Set the root of Bipl Directory
:: This must be the directory containging bin\
@set BIPL_ROOT=C:\projects\bipl\
@set filename=%~f1
@set extention=%~x1
@set outputfilename=%~n1.exe

:: Check whether BIPL_ROOT is valid.
@if not exist %BIPL_ROOT%\bipl.sln goto wrongrootdir
:: Currently, only one argument needed
@if "%filename%"=="" goto usage
:: Test whether the file exists.
@if not exist %filename% goto filenotexist
:: Check file extention
@if not "%extention%"==".bipl" goto wrongfilename
@goto parse

:: error handling
:wrongrootdir
@echo Current BIPL root directory: %BIPL_ROOT%
@echo Please set to the directory contains bipl.sln
@echo.
@goto:EOF

:usage
@echo Invalid argument: %1
@echo.
@echo Usage:  %~n0  "<script file name>"
@goto:EOF

:filenotexist
@echo "%1" not exist
@goto:EOF

:wrongfilename
@echo File name should be *.bipl
@goto:EOF

:: -----------------------------------------------------------------------

:parse
@echo bipl script file : %1
@echo set BIPL_ROOT %BIPL_ROOT%
@set currentDir=%~dp0
@pushd %BIPL_ROOT%\bipl_cxx\

%BIPL_ROOT%\bin\bipl_frontend.exe %currentDir%%1

@if %errorlevel%==0 goto vcsetting
@echo Bipl parsing error, STOP.
@popd
@goto:EOF


:: -----------------------------------------------------------------------
:: start setting the environment for building
:vcsetting
@popd

@echo Setting environment for using Microsoft Visual Studio 2010 x86 tools.

@call :GetVSCommonToolsDir
@if "%VS100COMNTOOLS%"=="" goto error_no_VS100COMNTOOLSDIR

@call "%VS100COMNTOOLS%VCVarsQueryRegistry.bat" 32bit No64bit

@if "%VSINSTALLDIR%"=="" goto error_no_VSINSTALLDIR
@if "%FrameworkDir32%"=="" goto error_no_FrameworkDIR32
@if "%FrameworkVersion32%"=="" goto error_no_FrameworkVer32
@if "%Framework35Version%"=="" goto error_no_Framework35Version

@set FrameworkDir=%FrameworkDir32%
@set FrameworkVersion=%FrameworkVersion32%

@if not "%WindowsSdkDir%" == "" (
	@set "PATH=%WindowsSdkDir%bin\NETFX 4.0 Tools;%WindowsSdkDir%bin;%PATH%"
	@set "INCLUDE=%WindowsSdkDir%include;%INCLUDE%"
	@set "LIB=%WindowsSdkDir%lib;%LIB%"
)

::
:: Root of Visual Studio IDE installed files.
::
@set DevEnvDir=%VSINSTALLDIR%Common7\IDE\

:: PATH
:: ----
@if exist "%VSINSTALLDIR%Team Tools\Performance Tools" (
	@set "PATH=%VSINSTALLDIR%Team Tools\Performance Tools;%PATH%"
)
@if exist "%ProgramFiles%\HTML Help Workshop" set PATH=%ProgramFiles%\HTML Help Workshop;%PATH%
@if exist "%ProgramFiles(x86)%\HTML Help Workshop" set PATH=%ProgramFiles(x86)%\HTML Help Workshop;%PATH%
@if exist "%VCINSTALLDIR%VCPackages" set PATH=%VCINSTALLDIR%VCPackages;%PATH%
@set PATH=%FrameworkDir%%Framework35Version%;%PATH%
@set PATH=%FrameworkDir%%FrameworkVersion%;%PATH%
@set PATH=%VSINSTALLDIR%Common7\Tools;%PATH%
@if exist "%VCINSTALLDIR%BIN" set PATH=%VCINSTALLDIR%BIN;%PATH%
@set PATH=%DevEnvDir%;%PATH%

@if exist "%VSINSTALLDIR%VSTSDB\Deploy" (
	@set "PATH=%VSINSTALLDIR%VSTSDB\Deploy;%PATH%"
)

@if not "%FSHARPINSTALLDIR%" == "" (
	@set "PATH=%FSHARPINSTALLDIR%;%PATH%"
)

:: INCLUDE
:: -------
@if exist "%VCINSTALLDIR%ATLMFC\INCLUDE" set INCLUDE=%VCINSTALLDIR%ATLMFC\INCLUDE;%INCLUDE%
@if exist "%VCINSTALLDIR%INCLUDE" set INCLUDE=%VCINSTALLDIR%INCLUDE;%INCLUDE%

:: LIB
:: ---
@if exist "%VCINSTALLDIR%ATLMFC\LIB" set LIB=%VCINSTALLDIR%ATLMFC\LIB;%LIB%
@if exist "%VCINSTALLDIR%LIB" set LIB=%VCINSTALLDIR%LIB;%LIB%

:: LIBPATH
:: -------
@if exist "%VCINSTALLDIR%ATLMFC\LIB" set LIBPATH=%VCINSTALLDIR%ATLMFC\LIB;%LIBPATH%
@if exist "%VCINSTALLDIR%LIB" set LIBPATH=%VCINSTALLDIR%LIB;%LIBPATH%
@set LIBPATH=%FrameworkDir%%Framework35Version%;%LIBPATH%
@set LIBPATH=%FrameworkDir%%FrameworkVersion%;%LIBPATH%

@goto end

:: -----------------------------------------------------------------------
:GetVSCommonToolsDir
@set VS100COMNTOOLS=
@call :GetVSCommonToolsDirHelper32 HKLM > nul 2>&1
@if errorlevel 1 call :GetVSCommonToolsDirHelper32 HKCU > nul 2>&1
@if errorlevel 1 call :GetVSCommonToolsDirHelper64  HKLM > nul 2>&1
@if errorlevel 1 call :GetVSCommonToolsDirHelper64  HKCU > nul 2>&1
@exit /B 0

:GetVSCommonToolsDirHelper32
@for /F "tokens=1,2*" %%i in ('reg query "%1\SOFTWARE\Microsoft\VisualStudio\SxS\VS7" /v "10.0"') DO (
	@if "%%i"=="10.0" (
		@SET "VS100COMNTOOLS=%%k"
	)
)
@if "%VS100COMNTOOLS%"=="" exit /B 1
@SET "VS100COMNTOOLS=%VS100COMNTOOLS%Common7\Tools\"
@exit /B 0

:GetVSCommonToolsDirHelper64
@for /F "tokens=1,2*" %%i in ('reg query "%1\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\SxS\VS7" /v "10.0"') DO (
	@if "%%i"=="10.0" (
		@SET "VS100COMNTOOLS=%%k"
	)
)
@if "%VS100COMNTOOLS%"=="" exit /B 1
@SET "VS100COMNTOOLS=%VS100COMNTOOLS%Common7\Tools\"
@exit /B 0

:: -----------------------------------------------------------------------
:error_no_VS100COMNTOOLSDIR
@echo ERROR: Cannot determine the location of the VS Common Tools folder.
@goto end

:error_no_VSINSTALLDIR
@echo ERROR: Cannot determine the location of the VS installation.
@goto end

:error_no_FrameworkDIR32
@echo ERROR: Cannot determine the location of the .NET Framework 32bit installation.
@goto end

:error_no_FrameworkVer32
@echo ERROR: Cannot determine the version of the .NET Framework 32bit installation.
@goto end

:error_no_Framework35Version
@echo ERROR: Cannot determine the .NET Framework 3.5 version.
@goto end

:end

@pushd %BIPL_ROOT%

msbuild /t:bipl_backend /verbosity:m /nologo /p:Configuration=Debug /p:BuildProjectReferences=false

@if %errorlevel%==0 goto copyfileout
@echo Bipl build error, STOP.
@popd
@goto:EOF

:copyfileout
@pushd %BIPL_ROOT%\\bin

::
:: Since we do not put dynamic link library into system path,
:: following lines are required.
:: COPY DLL START
@xcopy *.dll %currentDir% /Q /Y >nul
@xcopy *.xml %currentDir% /Q /Y >nul
:: COPY DLL END

@xcopy bipl_backend.exe %currentDir% /Q /Y >nul

@popd
@popd
@del "%outputfilename%" /F /Q >nul
@rename bipl_backend.exe "%outputfilename%"
@echo Generate "%outputfilename%".